﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ArcEngine.Resources;

namespace ArcEngine.Editor.Wizards
{
	public partial class NewTextureWizard : Form
	{
		public NewTextureWizard()
		{
			InitializeComponent();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void NewTextureWizard_OnFormClosing(object sender, FormClosingEventArgs e)
		{
			// 
			if (DialogResult != DialogResult.OK)
				return;

			// Level already exists ?
			if (ResourceManager.GetTexture(TextureNameBox.Text) != null || TextureNameBox.Text == "")
			{
				MessageBox.Show("Texture name already in use or invalid. Use another name !");
				e.Cancel = true;
				return;
			}

			TexturePreviewBox.Image.Dispose();


			// Adds the texture file as a binary
			Binary bin = ResourceManager.CreateBinary(textureFile);
			if (bin != null)
				bin.LoadFile(textureFullPath);


			// Create the texture
			Texture texture = ResourceManager.CreateTexture(TextureNameBox.Text);
			if (texture != null)
				texture.LoadImage(textureFile);


		}


		/// <summary>
		/// Loads an external texture
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadTextureButton_Click(object sender, EventArgs e)
		{
			OpenFileDialog dlg = new OpenFileDialog();
			dlg.Filter = "Texture (*.png)|*.png|All Files (*.*)|*.*";
			dlg.Title = "Select a texture to open...";
			dlg.DefaultExt = ".png";

			if (dlg.ShowDialog() != DialogResult.OK)
				return;

			textureFile = dlg.SafeFileName;
			textureFullPath = dlg.FileName;
			TexturePreviewBox.Image = Image.FromFile(dlg.FileName);

		}


		/// <summary>
		/// Full path of the texture
		/// </summary>
		string textureFullPath;


		/// <summary>
		/// Name of the texture file
		/// </summary>
		string textureFile;

	}
}
